package com.example.qu_tao.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.qu_tao.entity.CaiGouInfo;
import com.example.qu_tao.R;

import java.util.List;

public class CaiGouAdapter extends RecyclerView.Adapter<CaiGouAdapter.ViewHolder> {

    private Context context;
    private List<CaiGouInfo> caiGouInfoList;

    public CaiGouAdapter(List<CaiGouInfo> caiGouInfoList) {
        this.caiGouInfoList = caiGouInfoList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_caigou, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CaiGouInfo info = caiGouInfoList.get(position);
        holder.tvType.setText("货品类型: " + info.getType());
        holder.tvVariety.setText("货品品种: " + info.getVariety());
        holder.tvQuantity.setText("需求量: " + info.getQuantity() + info.getUnit());
        holder.tvSource.setText("期望货源地: " + info.getSource());
        holder.tvCycle.setText("采购周期: " + info.getCycle());
        holder.tvRegion.setText("收货地区: " + info.getRegion());
        holder.tvTime.setText("发布时间: " + info.getPublishTime());
    }

    @Override
    public int getItemCount() {
        return caiGouInfoList.size();
    }

    public void updateData(List<CaiGouInfo> newData) {
        caiGouInfoList.clear();
        caiGouInfoList.addAll(newData);
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvType, tvVariety, tvQuantity, tvSource, tvCycle, tvRegion, tvTime;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvType = itemView.findViewById(R.id.type);
            tvVariety = itemView.findViewById(R.id.variety);
            tvQuantity = itemView.findViewById(R.id.quantity);
            tvSource = itemView.findViewById(R.id.source);
            tvCycle = itemView.findViewById(R.id.cycle);
            tvRegion = itemView.findViewById(R.id.region);
            tvTime = itemView.findViewById(R.id.time);
        }
    }
}